/*****************************************************************************
* Copyright (c) 2014,H.B.
* All rights reserved.
* 
* FileName : DtvTuner.java
* Description : define DtvTuner
* 
* Version : 0.0.0
* Author : HB   Reviewer : 
* Date : 2014-07-07
* Record : Create
*
* History : 
* (1) Version : 0.0.0
*     Author : HB   Reviewer : 
*     Date : 2014-07-07
*     Record : Create
****************************************************************************/

package hb.dtv;


public class DtvTuner
{
	static public final int DVBC	=	0x0001;
	static public final int DVBC2	=	0x0002;
	static public final int DVBS	=	0x0004;
	static public final int DVBS2	=	0x0008;
	static public final int DVBT	=	0x0010;
	static public final int DVBT2	=	0x0020;
	static public final int ATSCC 	=	0x0040;
	static public final int ATSCT 	=	0x0080;
	static public final int ISDBC	=	0x0100;
	static public final int ISDBS	=	0x0200;
	static public final int ISDBT	=	0x0400;

	static public final int QAM16	=	0;
	static public final int QAM32	=	1;
	static public final int QAM64	=	2;
	static public final int QAM128	=	3;
	static public final int QAM256	=	4;
	static public final int QAM512	=	5;
	static public final int QAM1024	=	6;
	static public final int QAM2048	=	7;
	static public final int QAM4096	=	8;
	
	static public final int SPECTRUM_NORMAL		=	0;
	static public final int SPECTRUM_INVERSE	=	1;
	static public final int SPECTRUM_AUTO		=	2;

	public static class DvbcPamameter
	{
		public int frequency;//in KHz
		public int symbolRate;
		public int qam;
		public int spectrum;

		public DvbcPamameter(int frequency,int symbolRate,
			int qam,int spectrum)
		{
			this.frequency=frequency;
			this.symbolRate=symbolRate;
			this.qam=qam;
			this.spectrum=spectrum;
		}
	}

	public static class Status
	{
		public boolean locked;
		public int strength;// in percent
		public int qulity;// in percent
		public int power;// in dBuV/100
		public int snr;//Signal to noise ratio in dB/100
		public int ber;// bit error rate in 1e-7

		private Status(boolean locked,int strength,int qulity,
			int power,int snr,int ber)
		{
			this.locked=locked;
			this.strength=strength;
			this.qulity=qulity;
			this.power=power;
			this.snr=snr;
			this.ber=ber;
		}
	}

	final int mJniPtr;

	DtvTuner(int ptr)
	{
		mJniPtr=ptr;
	}

	public void Release()
	{
		nativeRelease(mJniPtr);
	}
	
	public boolean SetParameter(DvbcPamameter param)
	{
		return nativeSetParameter(mJniPtr,param);
	}
	
	public DvbcPamameter GetDvbcParameter()
	{
		return nativeGetDvbcParameter(mJniPtr);
	}

	public boolean GetLocked()
	{
		return nativeGetLocked(mJniPtr);
	}

	public Status GetStatus()
	{
		return nativeGetStatus(mJniPtr);
	}

	public int GetIndex()
	{
		return nativeGetIndex(mJniPtr);
	}

	@Override
	public boolean equals(Object o) 
	{
		 if (this == o) return true;
		 if (!(o instanceof DtvTuner)) return false;
		 DtvTuner d=(DtvTuner)o;
		 if(mJniPtr==d.mJniPtr)return true;
		 return GetIndex()==d.GetIndex();
	}

	@Override
	protected void finalize() throws Throwable 
	{
		nativeDestroy(mJniPtr);
		super.finalize();
	}
	
	private native static boolean nativeSetParameter(int jniPtr,DvbcPamameter param);
	
	private native static DvbcPamameter nativeGetDvbcParameter(int jniPtr);

	private native static boolean nativeGetLocked(int jniPtr);

	private native static Status nativeGetStatus(int jniPtr);

	private native static int nativeGetIndex(int jniPtr);
	
	private native static void nativeRelease(int jniPtr);
	
	private native static void nativeDestroy(int jniPtr);
}

